<template>
  <div class="request-detail" :class="`type-${type}`">
    <div class="detail-header">
      <span class="detail-icon">{{ icon }}</span>
      <span class="detail-label">{{ label }}:</span>
    </div>
    <div class="detail-value">
      <code v-if="type === 'json'" class="json-code">{{ formattedValue }}</code>
      <code v-else-if="type === 'url'" class="url-code">{{ value }}</code>
      <span v-else-if="type === 'method'" class="method-badge" :class="`method-${String(value).toLowerCase()}`">
        {{ value }}
      </span>
      <span v-else>{{ value }}</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  label: string
  value: string | number | object
  icon: string
  type?: 'url' | 'method' | 'json' | 'text'
}

const props = withDefaults(defineProps<Props>(), {
  type: 'text'
})

const formattedValue = computed(() => {
  if (props.type === 'json') {
    try {
      const obj = typeof props.value === 'string' ? JSON.parse(props.value as string) : props.value
      return JSON.stringify(obj, null, 2)
    } catch {
      return String(props.value)
    }
  }
  return String(props.value)
})
</script>

<style scoped>
.request-detail {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
  transition: all 0.2s ease;
}

.request-detail:hover {
  border-color: #d1d5db;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.detail-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.detail-icon {
  font-size: 1rem;
}

.detail-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: #4b5563;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.detail-value {
  margin-left: 24px;
}

/* JSON 代码样式 */
.json-code {
  display: block;
  background: #1e293b;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 6px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.8rem;
  line-height: 1.4;
  overflow-x: auto;
  white-space: pre;
}

/* URL 代码样式 */
.url-code {
  background: #f3f4f6;
  color: #1f2937;
  padding: 6px 10px;
  border-radius: 4px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.85rem;
  word-break: break-all;
}

/* 方法标签样式 */
.method-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 4px;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.method-get {
  background: #d1fae5;
  color: #065f46;
}

.method-post {
  background: #dbeafe;
  color: #1e40af;
}

.method-put {
  background: #fef3c7;
  color: #92400e;
}

.method-delete {
  background: #fee2e2;
  color: #991b1b;
}

/* 类型特定样式 */
.type-url .detail-value {
  word-break: break-all;
}

.type-json .detail-header {
  margin-bottom: 12px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .request-detail {
    padding: 12px;
  }
  
  .json-code {
    font-size: 0.75rem;
    padding: 10px;
  }
  
  .detail-value {
    margin-left: 0;
    margin-top: 8px;
  }
  
  .detail-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}
</style>
